<?php

require_once 'Zend/Auth.php';
require_once 'Zend/Auth/Adapter/DbTable.php';

class Place_IndexController extends Zend_Controller_Action{
    
    
    public function indexAction(){
        $request = $this->getRequest();
        $this->view->assign('action',$request->getBaseURL()."/admin/index/login/");
        $this->view->assign('userName', 'User Name'); 
        $this->view->assign('password', 'Password');  
    }
    
    public function loginAction(){
        $this->_helper->viewRenderer->setNoRender(true);
        $registry   = Zend_Registry::getInstance();
        $constants = $registry->constants;
        try{
            if(isset($_POST)){
                $registry   = Zend_Registry::getInstance();
                $userName = $this->_request->getParam('userName');
                $password = $this->_request->getParam('password');
                
                $normalUtils = new My_Utils_NormalUtils();
                $DB = $normalUtils->getNormalDb(1);
                
                $query = "select sale_user_id, login_name, full_name, phone, email, password, active  from sale_user".
                        " where login_name='".$userName."' and password='".$password."'"." and active = 1";
                $sql=$DB->query($query);
                $result = $sql->fetchAll();
                if($result){
                    Zend_Session::start();
                    $ss = new Zend_Session_Namespace('user_session');
                    $ss->user = $result[0];
                    echo $registry->constants->SUCCESS_STRING;
                }else{
                    echo $registry->constants->USER_AND_PASS_NOT_MATCH;
                }
            }
        }catch(Exception $e){
            $writer = new Zend_Log_Writer_Stream(BASE_PATH.$constants->LOG_FOLDER.'production_'.date('Y_m_d').$constants->LOG_FILE_EXTENSION);
            $logger = new Zend_Log($writer);
            $controller = $this->getRequest()->getControllerName();
            $action = $this->getRequest()->getActionName();
            $logger -> info($controller."|".$action."|".$e -> getMessage());
            echo $constants->ERROR_STRING;
        }
        
        
    }
    
    public function loginedAction(){
        $this->_helper->viewRenderer->setNoRender(true);
        $registry   = Zend_Registry::getInstance();
        $constants = $registry->constants;
        $userSession = new Zend_Session_Namespace('user_session');
        $user = $userSession->user;
        try{
            if($user->full_name != '' && $user->sale_user_id != ''){
                $userId = $user->sale_user_id;
                // $userRoleModel = new Admin_Model_SaleUserRole();
                // $userRole = $userRoleModel->getRoleOfUser($userId);
//                 
                // $roleFeatureModel = new Admin_Model_SaleUserRoleFeature();
                // $userRoleFeature = $roleFeatureModel->getFeatureOfUserRole($userRole);
                // $this->_redirect($userRoleFeature[0]->feature_url);
                
                $userRoleModel = new Place_Model_SaleUserRole();
                $userRole = $userRoleModel->getRoleOfUser($userId);
                
                $saleUserRoleFeature = new Place_Model_SaleUserRoleFeature();
                $featureArr = $saleUserRoleFeature->getFeatureOfUserRole($userRole);
                $this->_redirect($featureArr[0]->feature_url);
                
                // $this->_redirect('/place/merchant-place/index');
            }else{
                $this->_redirect('/place/index/index');
            }   
        }catch(Exception $e){
            $writer = new Zend_Log_Writer_Stream(BASE_PATH.$constants->LOG_FOLDER.'production_'.date('Y_m_d').$constants->LOG_FILE_EXTENSION);
            $logger = new Zend_Log($writer);
            $controller = $this->getRequest()->getControllerName();
            $action = $this->getRequest()->getActionName();
            $logger -> info($controller."|".$action."|".$e -> getMessage());
            echo $constants->ERROR_STRING;
        }
            
    }
    
    
    public function changePasswordAction(){
        $registry   = Zend_Registry::getInstance();
        $constants = $registry->constants;
        $userSession = new Zend_Session_Namespace('user_session');
        $user = $userSession->user;
        $userId = $user->sale_user_id;
        try{
            if(isset($userId)){
                // $userRoleModel = new Admin_Model_SaleUserRole();
                // $userRole = $userRoleModel->getRoleOfUser($userId);
//                 
                // $roleFeatureModel = new Admin_Model_SaleUserRoleFeature();
                // $userRoleFeature = $roleFeatureModel->getFeatureOfUserRole($userRole);
                // $this->view->userFeature = $userRoleFeature;
                // $this->view->userName = $user->full_name;   
            }else{
                $this->_redirect('/admin/index');
            }
        }catch(Exception $e){
            $writer = new Zend_Log_Writer_Stream(BASE_PATH.$constants->LOG_FOLDER.'production_'.date('Y_m_d').$constants->LOG_FILE_EXTENSION);
            $logger = new Zend_Log($writer);
            $controller = $this->getRequest()->getControllerName();
            $action = $this->getRequest()->getActionName();
            $logger -> info($controller."|".$action."|".$e -> getMessage());
            echo $constants->ERROR_STRING;
        }
        
        
    }
    
    public function changePassAction(){
        $this->_helper->viewRenderer->setNoRender(true);
        $registry   = Zend_Registry::getInstance();
        $constants = $registry->constants;
        $userSession = new Zend_Session_Namespace('user_session');
        $user = $userSession->user;
        $userId = $user->sale_user_id;
        try{
            $oldPass = $_POST['old_password'];
            $newPass = $_POST['new_password'];
            $newPassConfirm = $_POST['password_confirm'];
            
            $saleUserModel = new Admin_Model_SaleUser();
            if(($saleUserModel->checkPassword($userId, $oldPass)) == 0){
                echo $constants->PASSWORD_NOT_MATCH;
            }else{
                if($newPass != $newPassConfirm){
                    echo $constants->PASSWORD_NOT_MATCH2;
                }else{
                    $arrParams = array(
                                    "sale_user_id"=>$userId,
                                    "password"=>$newPass);
                    $saleUserModel->changePassword($arrParams);
                    echo $constants->CHANGE_PASS_SUCCESS;
                }
            }
        }catch(Exception $e){
            $writer = new Zend_Log_Writer_Stream(BASE_PATH.$constants->LOG_FOLDER.'production_'.date('Y_m_d').$constants->LOG_FILE_EXTENSION);
            $logger = new Zend_Log($writer);
            $controller = $this->getRequest()->getControllerName();
            $action = $this->getRequest()->getActionName();
            $logger -> info($controller."|".$action."|".$e -> getMessage());
            echo $constants->ERROR_STRING;
        }
        
    }
    
    public function logoutAction(){
        $this->_helper->viewRenderer->setNoRender(true);
        Zend_Session::destroy();
        $this->_redirect('/place/index');
    }
    
    
} 